<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Home extends Admin_Controller {

	function __construct ()
	{
		parent::__construct();
        $this->load->model('project_m');
        $this->load->model('userinto_m');
        $this->load->model('settlement_m');
        $this->load->model('charge_m');
		if(!$this->auth->is_admins()){
			redirect(site_url('user/login'));
		}
	}
	
	public function index()
	{
        $user_id = $this->session->userdata('user_id');
        $user = $this->user_m->get_user_by_user_id($user_id);
        if($user['user_group']==4){
            //项目总数
            $data['allproject']=$this->project_m->count_user_project($user_id);
            //完成项目数
            $data['complete']=$this->project_m->count_user_sucess_project($user_id);
            //未完成的
            $data['completing']=$this->project_m->count_user_ing_project($user_id);
            //计算
            $userinto = $this->userinto_m->get_userinto_money($user_id);
            $allmoney=0;
            if(!empty($userinto)){
                foreach ($userinto as $key=>$val){
                    $allmoney += $val['project_money']*$val['into_num'];
                }
            }
            //应结算
            $data['allmoney'] = $allmoney;
            //已结算
            $money = $this->settlement_m->get_project_settlement($user_id);
            $moneys=0;
            if(!empty($money)){
                foreach ($money as $key=>$val){
                    $moneys += $val['settlement_money'];
                }
            }
            $data['money'] = $moneys;

        }else{
            //项目总数
            $data['allproject']=$this->project_m->count_user_project();
            //完成项目数
            $data['complete']=$this->project_m->count_user_sucess_project();
            //未完成的
            $data['completing']=$this->project_m->count_user_ing_project();
            //计算
            $allmoney=$this->project_m->count_project_totla_monney();
            //应结算
            $data['allmoney'] = $allmoney;
            //已结算
            $moneys = $this->charge_m->count_project_charge();
            $data['money'] = $moneys;
        }

		$data['title'] = '默认首页';
		$data['siderbar'] = 'home';
		$data['submenu'] = 'home';
        $this->load->view('index/home', $data);
	}
}